package com.rwy.util;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;

import com.alibaba.fastjson.JSON;
import com.rwy.util.constant.CacheKeyEnum;
import com.rwy.util.constant.CacheValueKeyEnum;
import com.rwy.web.filter.authorize.AuthorizeRespDTO;
import com.tool.http.httpclient.HttpClientUtil;
import com.tool.util.http.common.HttpConfig;

import lombok.extern.log4j.Log4j;

/**
 * 
 * @author chenj
 * @datetime 2016年5月5日 下午4:03:24
 * @desc 资金 抽象类
 *
 */
@Log4j
public abstract class CommonController extends BaseController {
	
	public static DecimalFormat df  = new DecimalFormat("0.##");
	/**
	 * 操作数据成功提示
	 */
	public static final String message_operation_success = "harson.service.operator.success";
	public static final String message_operation_fail ="harson.service.operator.fail";

	/** 逗号分割符*/
	public static final String comma_prefix =",";
	
	protected static final String begining_of_month_key = "beginingMonth";
	protected static final String current_date_key = "currnetDate";
	
	public static final String get_department_user_lsit_url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=access_token_value&fetch_child=1&status=0&department_id=";
	
	@Value("${login_get_token}")
	public String login_get_token;
	
	@Value("${login_get_user_info}")
	public String login_get_user_info;
	
	@Value("${login_get_openId_by_userId}")
	public String login_get_openId_by_userId;
	
	@Value("${get_member_mesage_url}")
	public String get_member_mesage_url;
	
	@Value("${isShowEarnings}")
	public Boolean isShowEarnings;
	
	@Value("${get_jssdk_ticket}")
	public String get_jssdk_ticket;
	
	@Value("${fault_handle_login_get_token}")
	public String fault_handle_login_get_token;
	
	@Value("${fault_handle_agentid}")
	public int fault_handle_agentid;
	
	@Value("${fault_handle_advice_department}")
	public String fault_handle_advice_department;

	@Value("${fault_handle_advice_message}")
	public String fault_handle_advice_message;

	@Value("${fault_login_url}")
	public String fault_login_url;
	
	@Value("${api_domain}")
	public String api_domain;
	
	@Value("${login_fault_redirect_url}")
	public String login_fault_redirect_url;
	
	@Value("${upload_image_url}")
	public String upload_image_url;
	
	protected void setJsonMesasge(JsonObject json) {
		 setJsonMesasge(json, true);
	}
	
	protected void setJsonMesasge(JsonObject json, boolean pass) {
		if (!pass) {
			json.setMessage("操作失败");
			json.setStatus(JsonObject.STATUS_FAIL);
		} else {
			json.setMessage("操作成功");
			json.setStatus(JsonObject.STATUS_SUCCESS);
		}
	}
	

	
	/**
	 * 
	 *@author chenj
	 *@datetime 2016年6月28日 上午11:23:46
	 *@desc 小数点格式化数据, 两位小数点, 四舍五入
	 *@param data
	 *@return
	 */
	public static String formatDouble(double data) {
		return df.format(data);
	}
	
	/** 获取当月区间, 月初日期, 当前日期*/
	protected Map<String, String> getCurrentMonthInterval() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Map<String, String> data = new HashMap<String, String>();
		data.put(current_date_key,DateUtil.getFormatString(DateUtil.getCurrentDate(), DateUtil.SIMPLE_PATTERN));
		data.put(begining_of_month_key, sdf.format(DateUtil.getCurrentDate())+"-01");
		return data;
	}
	
	protected  AuthorizeRespDTO getCommonAccessToken() throws Exception {
		AuthorizeRespDTO tokenDto = getCache(CacheKeyEnum.access_token.getKey(), CacheValueKeyEnum.common_access_token.getKey(), AuthorizeRespDTO.class);
		if (ObjectUtil.isEmpty(tokenDto)) {
			 String data = HttpClientUtil.get(HttpConfig.custom().url(login_get_token));
			 tokenDto = JSON.parseObject(data, AuthorizeRespDTO.class);
		     
			 if (ObjectUtil.isEmpty(tokenDto) || StringUtil.isEmpty(tokenDto.getAccess_token())) {
		    	 tokenDto = null;
		    	 log.error("get access token fail .." + data);
		     } else {
		    	 addCache(CacheKeyEnum.access_token.getKey(), CacheValueKeyEnum.common_access_token.getKey(), tokenDto);
		     }
		}
		return tokenDto;
	}
	
	public AuthorizeRespDTO getFaultAccessToken() throws Exception {
		AuthorizeRespDTO tokenDto = getCache(CacheKeyEnum.access_token.getKey(), CacheValueKeyEnum.cache_fault_device_key.getKey(), AuthorizeRespDTO.class);
		if (ObjectUtil.isEmpty(tokenDto)) {
			// String data = HttpClientUtil.get(HttpConfig.custom().url(login_get_token));
			  String data = HttpClientUtil.get(HttpConfig.custom().url(fault_handle_login_get_token));
			 tokenDto = JSON.parseObject(data, AuthorizeRespDTO.class);
		     
			 if (ObjectUtil.isEmpty(tokenDto) || StringUtil.isEmpty(tokenDto.getAccess_token())) {
		    	 tokenDto = null;
		    	 log.error("get access token fail .." + data);
		     } else {
		    	 addCache(CacheKeyEnum.access_token.getKey(), CacheValueKeyEnum.cache_fault_device_key.getKey(), tokenDto);
		     }
		}
		return tokenDto;
	}
}
